package com.zhiyizhen.handheld.component.pharmacy.view;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.zhiyizhen.handheld.R;
import com.zhiyizhen.handheld.common.ResourcesUtil;
import com.zhiyizhen.handheld.component.pharmacy.bean.GroupDetailListBean;

/**
 * description
 */
public class RegimenShuyeUnitLayout extends LinearLayout {

    private TextView seqTv;
    private TextView drugInfoTv;
    private EditText thisTimeCountTv;
    private TextView totalCountTv;
    private TextView thisTimeCountUnitTv;
    private TextView remarkTv;
    private TextView hasGiveTv;
    private TextView usageCountTv;

    private GroupDetailListBean detailListBean;

    public RegimenShuyeUnitLayout(Context context, int seq, GroupDetailListBean detailListBean) {
        super(context);
        initView(context);
        this.detailListBean = detailListBean;
        String drugInfo = getStringBuilder(detailListBean).toString();
        String usage = detailListBean.getDosage() + detailListBean.getDosageUnitText();
        String remark = detailListBean.getRemark();
        String totalCount = detailListBean.getTotalOutQty() + detailListBean.getTotalOutQtyUnitText();
        String hasGive = detailListBean.getDispenseQty() + detailListBean.getTotalOutQtyUnitText();
        String thisTimeCount = String.valueOf(detailListBean.getTotalOutQty() - detailListBean.getDispenseQty());
        setData(seq, drugInfo, usage, remark, totalCount, hasGive, thisTimeCount, detailListBean.getTotalOutQtyUnitText());
    }

    @NonNull
    private static StringBuilder getStringBuilder(GroupDetailListBean detailListBean) {
        StringBuilder drugInfoSb = new StringBuilder();
        drugInfoSb.append(detailListBean.getDrugName());
        drugInfoSb.append(" ");
        if(detailListBean.getDrugDosage() != null) {
            drugInfoSb.append(detailListBean.getDrugDosage())
                    .append(detailListBean.getDrugDosageUnitText())
                    .append("*");
        }
        drugInfoSb.append(detailListBean.getMinPackQty());
        drugInfoSb.append(detailListBean.getMinPackUnitText());
        drugInfoSb.append("/")
                .append(detailListBean.getPackUnitText());
        return drugInfoSb;
    }

    public RegimenShuyeUnitLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    private void initView(Context context) {
        inflate(context, R.layout.view_regimen_shuye_seq, this);
        setOrientation(LinearLayout.HORIZONTAL);
        seqTv = findViewById(R.id.seqTv);
        usageCountTv = findViewById(R.id.usageCountTv);
        drugInfoTv = findViewById(R.id.drugInfoTv);
        hasGiveTv = findViewById(R.id.hasGiveTv);
        thisTimeCountTv = findViewById(R.id.thisTimeCountTv);
        thisTimeCountUnitTv = findViewById(R.id.thisTimeCountUnitTv);
        totalCountTv = findViewById(R.id.totalCountTv);
        remarkTv = findViewById(R.id.remarkTv);

        thisTimeCountTv.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                detailListBean.setCurrentDispenseQty(thisTimeCountTv.getText().toString());
            }
        });
    }

    private void setData(int seq, String drugInfo, String usage, String remark, String totalCount, String hasGive, String thisTimeCount, String thisTimeCountUnit) {
        seqTv.setText(String.valueOf(seq));
        drugInfoTv.setText(drugInfo);
        usageCountTv.setText(usage);
        if (TextUtils.equals(thisTimeCount, "0")) {
            thisTimeCountTv.setEnabled(false);
            thisTimeCountTv.setBackgroundColor(ResourcesUtil.getColor(R.color.translucent_background));
        } else {
            thisTimeCountTv.setEnabled(true);
            thisTimeCountTv.setBackground(ResourcesUtil.getDrawable(R.drawable.bg_regimen_edit));

        }
        thisTimeCountTv.setText(thisTimeCount);
        totalCountTv.setText(totalCount);
        remarkTv.setText(remark);
        hasGiveTv.setText(hasGive);
        thisTimeCountUnitTv.setText(thisTimeCountUnit);
    }


}